package com.ling.common.library.utils

import android.annotation.SuppressLint
import android.view.MotionEvent
import android.view.View
import androidx.annotation.DrawableRes

/**
 * @author Ling
 * @date :2024/11/27 15:09
 */
class TouchClickUtil private constructor(): View.OnTouchListener{

    private var isLongClick = false
    //    private var outMove = false
    private var mTargetView: View? = null
    private var mParentView: View? = null
    @DrawableRes
    private var mRootViewDefaultBg = 0
    @DrawableRes
    private var mRootViewTouchingBg = 0
    private var dx = 0f
    private var dy = 0f
    private var mScale = 0f

    private var onClickCallback : (() -> Unit)? = null
    private var onLongClickCallback :((isDown : Boolean) -> Unit)? = null
    private var onTouchEventCallback : ((isDown : Boolean) -> Unit)? = null

    @SuppressLint("ClickableViewAccessibility")
    constructor(targetView: View) : this() {
        this.mTargetView = targetView
        this.mTargetView?.setOnTouchListener(this)
    }

    companion object{
        fun getNewInstance(targetView: View): TouchClickUtil {
            return TouchClickUtil(targetView)
        }
    }

    fun setParentView(view: View): TouchClickUtil {
        this.mParentView = view
        return this
    }

    fun setParentDefaultBg(@DrawableRes drawable: Int): TouchClickUtil {
        this.mRootViewDefaultBg = drawable
        return this
    }

    fun setParentTouchingBg(@DrawableRes drawable: Int): TouchClickUtil {
        this.mRootViewTouchingBg = drawable
        return this
    }

    fun setTouchViewClickListener(onClickCallback: () -> Unit): TouchClickUtil {
        this.onClickCallback = onClickCallback
        return this
    }

    fun setTouchViewLongClickListener(onLongClickCallback: (isDown : Boolean) -> Unit): TouchClickUtil {
        mTargetView?.setOnLongClickListener { _ ->
            isLongClick = true
            onLongClickCallback(true)
            true
        }
        this.onLongClickCallback = onLongClickCallback
        return this
    }

    fun setTouchViewEventListener(onTouchEventCallback: (isDown : Boolean) -> Unit): TouchClickUtil {
        this.onTouchEventCallback = onTouchEventCallback
        return this
    }

    fun setTouchScaleEffect(scale : Float): TouchClickUtil {
        mScale = scale
        return this
    }



    @SuppressLint("ClickableViewAccessibility")
    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        when (event?.action) {
            MotionEvent.ACTION_DOWN -> {
                if(mScale != 0f){
                    v?.scaleX = mScale
                    v?.scaleY = mScale
                }
                isLongClick = false
//                outMove = false
                dx = event.x
                dy = event.y
                mParentView?.setBackgroundResource(mRootViewTouchingBg)
                onTouchEventCallback?.let {
                    it(true)
                }
            }
            //如果需要对移动范围进行控制，则需要在这里进行一些排除，TODO
//            MotionEvent.ACTION_MOVE -> {
//                if (abs((event.x - dx).toDouble()) > 20 || abs((event.y - dy).toDouble()) > 20) {
//                    outMove = true
//                }
//            }

            MotionEvent.ACTION_CANCEL -> {
                if(mScale != 0f){
                    v?.scaleX = 1f
                    v?.scaleY = 1f
                }
                mParentView?.setBackgroundResource(mRootViewDefaultBg)
                if (isLongClick) {
                    onLongClickCallback?.let { it(false) }
                }
                onTouchEventCallback?.let { it(false) }
            }

            MotionEvent.ACTION_UP -> {
                if(mScale != 0f){
                    v?.scaleX = 1f
                    v?.scaleY = 1f
                }
                mParentView?.setBackgroundResource(mRootViewDefaultBg)
                if (!isLongClick) {
                    onClickCallback?.let { it() }
                }else{
                    onLongClickCallback?.let { it(false) }
                }
                onTouchEventCallback?.let { it(false) }
                isLongClick = false
//                outMove = false
            }
            else -> {}
        }
        return onLongClickCallback == null
    }
}